var editData="";
var editId=$.getUrlVar('id');
$(function(){
	
$.get("/index.php?m=Admin&c=Community&a=read&id="+editId,function(data){
	if(data.stat==1){
		editData=data.data;
		$("#no").attr("value",editData.no.substr(-4));
		$(".uid").attr("value",editData.id);
		$("#name").attr("value",editData.name);
		$("#address").attr("value",editData.address);
		$("#lng").attr("value",editData.lng);
		$("#lat").attr("value",editData.lat);
		$("#svr_start_time").attr("value",editData.svr_start_time);
		$("#svr_end_time").attr("value",editData.svr_end_time);
		$("#remark").attr("value",editData.remark);
		getDictEd(editData);
		 getAddress(editData)
	}else{
		console.log(data.msg);
	}
});

	//获取数据字典下拉
	function getDictEd(editData){
		var str="";
		$.post("/index.php?m=Admin&c=Dict&a=getdict",{type:"type"},function(data){
			if(data){
				for(x in data){
					if(data[x]['value']==editData.type){
						str+='<option value="'+data[x]['value']+'" selected="selected">'+data[x]['text']+'</option>';
					}else{
						str+='<option value="'+data[x]['value']+'">'+data[x]['text']+'</option>';
					}
					
				}
				$(".type").append(str);
			}
		});
	}


	//修改页地址联动
	function getAddress(editData){
		var addressAll=[];
		var info={
			province:0,
			city:0
		};
		$.get("/index.php?m=Admin&c=Common&a=getAddress",function(data){
			console.log(data);
			if(data){
				 // var data=$.parseJSON(data);
		          	 addressAll=data;
		         	  console.log(addressAll);
		         	  // 省份
		          	 var str="";
		          	 for(x in addressAll){
		          	 		if(addressAll[x].id==editData.province){
		          	 			str+='<option value="'+addressAll[x].id+'" selected="selected">'+addressAll[x].name+'</option>';
		          	 		}else{
		          	 			str+='<option value="'+addressAll[x].id+'">'+addressAll[x].name+'</option>';
		          	 		}
		          			
		          		}
		         	  $("#province").append(str);
		         	  //城市
		         	  var str="";
		         	  info.province=$("#province").find('option:selected').val();
		         	  var str='<option value="" checked="checked">请选择城市</option>';
		         	  var data=addressAll[info.province].data;
		         	  console.log(data);
		         	  for(x in data){
		         	  	if(data[x].id==editData.city){
		         	  		str+='<option value="'+data[x].id+'" selected="selected">'+data[x].name+'</option>';
		         	  	}else{
		         	  		str+='<option value="'+data[x].id+'">'+data[x].name+'</option>';
		         	  	}

		         	  }
		         	  $("#city").empty();
		         	  $("#city").append(str);
		         	  //区
		         	  var str="";
	         	 		info.city=$("#city").find('option:selected').val();
						var str='<option value="" checked="checked">请选择区</option>';
						var data=addressAll[info.province]['data'][info.city].data;
						console.log(data);
						for(x in data){
							if(data[x].id==editData.area){
								str+='<option value="'+data[x].id+'" selected="selected">'+data[x].name+'</option>';
							}else{
								str+='<option value="'+data[x].id+'">'+data[x].name+'</option>';
							}
							
						}
						$("#area").empty();
						$("#area").append(str);
		         	 //点击事件
						 $(document).on("change","#province",function(){
							info.province=$(this).find('option:selected').val();
							var str='<option value="" checked="checked">请选择城市</option>';
							var data=addressAll[info.province].data;
							console.log(data);
							for(x in data){
								if(data[x].id==editData.city){
									str+='<option value="'+data[x].id+'" selected="selected>'+data[x].name+'</option>';
								}else{
									str+='<option value="'+data[x].id+'">'+data[x].name+'</option>';
								}
								
							}
							$("#city").empty();
							$("#city").append(str);
						});
							// 第二级
						$(document).on("change","#city",function(){
							info.city=$(this).find('option:selected').val();

							var str='<option value="" checked="checked">请选择区</option>';
							var data=addressAll[info.province]['data'][info.city].data;
							console.log(data);
							for(x in data){
								if(data[x].id==editData.area){
									str+='<option value="'+data[x].id+'" selected="selected>'+data[x].name+'</option>';
								}else{
									str+='<option value="'+data[x].id+'">'+data[x].name+'</option>';
								}
								
							}
							$("#area").empty();
							$("#area").append(str);
						});
			}
		           
		});

	}
})
